package malus.shaofeng.com.beiwomusic.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.activity.BBSTopicActivity;
import malus.shaofeng.com.beiwomusic.adapter.PartTopicAdapter;
import malus.shaofeng.com.beiwomusic.model.HotPost;
import malus.shaofeng.com.beiwomusic.utils.Constants;
import malus.shaofeng.com.beiwomusic.utils.SPHelper;

/**
 * 参与的话题.
 */
public class SpaceMinePartFragment extends Fragment implements AdapterView.OnItemClickListener {

    ListView partList;
    private ArrayList<HotPost> data;
    private PartTopicAdapter adapter;

    public SpaceMinePartFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_space_mine_part, container, false);
        partList=(ListView)inflate.findViewById(R.id.part_topic_list);
        data = new ArrayList<>();
        adapter = new PartTopicAdapter(getActivity(), data);
        partList.setAdapter(adapter);
        partList.setOnItemClickListener(this);
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        //加载网络数据
        HttpUtils httpUtils=new HttpUtils();
        String url = "http://kxai.cn/qms/ws/bbs/myPost.json";
        RequestParams params = new RequestParams();
        params.addBodyParameter("token", SPHelper.getInstance().getToken());
        params.addBodyParameter("type","2");
        params.addBodyParameter("pageSize","10");
        params.addBodyParameter("page","1");
        httpUtils.send(
                HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String result = objectResponseInfo.result.toString();
                try {
                    JSONObject object=new JSONObject(result);
                    JSONObject response = object.getJSONObject("response");
                    JSONArray posts = response.getJSONArray("posts");
                    data.clear();
                    for (int i = 0; i < posts.length(); i++) {
                        JSONObject jsonObject = posts.getJSONObject(i);
                        HotPost post=new HotPost();
                        post.parseJSON(jsonObject);
                        data.add(post);
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), BBSTopicActivity.class);
        intent.putExtra(Constants.BundleKey.KEY_BBS_TOPIC,data.get(position));
        startActivity(intent);
    }
}
